﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace InSys.Documents
{
    public partial class FormSelectDocument : Form
    {
        private IEnumerable<document> queryDocument;
        private MapTestLinqDataContext db;
        private DateTime WorkDate = Program.gWorkDate;
        private document selectedDocument;

        public FormSelectDocument()
        {
            InitializeComponent();
            //db = new MapTestLinqDataContext();
        }

        public document SelectedDocument { get { return selectedDocument; } }
        public MapTestLinqDataContext DataContext
        {
            get
            {
                return db;
            }
            set
            {
                db = value;
            }
        }

        public event EventHandler SearchEvent;

        private void FireSearchEvent()
        {
            if (null == SearchEvent) return;
            EventArgs e = new EventArgs();
            SearchEvent(this, e);
        }

        private void ucSearchDocument1_ButtonSearchClickEvent(object sender, EventArgs e)
        {
            Search();
        }

        private void Search()
        {
            FormWaitWindow wnd;
            wnd = new FormWaitWindow();
            wnd.Description = "Поиск документа. Подождите";
            wnd.Show();
            documentBindingSource.DataSource = typeof(document);
            backgroundWorker.RunWorkerAsync();
            while (this.backgroundWorker.IsBusy)
            {
                // Keep UI messages moving, so the form remains 
                // responsive during the asynchronous operation.
                Application.DoEvents();
            }
            //ClearBindingSourceDocumentInfoPanel(); // TO DO 
            documentBindingSource.DataSource = queryDocument;
            wnd.Close();
            wnd = null;
        }

        private void ucSearchDocument1_Load(object sender, EventArgs e)
        {
            Search();
        }

        private void BackgroundInitData()
        {
            queryDocument = ClassWorkWithDB.getListDocument(db, WorkDate, ucSearchDocument1);
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundInitData();
        }

        private void SelectDocument()
        {
            selectedDocument = null;
            if (documentBindingSource.DataSource != null)
            {
                selectedDocument = documentBindingSource.Current as document;
            }

            DialogResult = System.Windows.Forms.DialogResult.OK;
            FireSearchEvent();
            //Close();
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            SelectDocument();
        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            SelectDocument();
        }
    }
}
